library(plotly)
library(ggplot2)
library(reshape2)

range_c <- 8

x_vals <- seq(0.01, range_c, length.out = 100)
y_vals <- seq(0.01, range_c, length.out = 100)

z_vals <- outer(x_vals, y_vals, function(x, y)
  exp(-x * y))

plot_ly(x = ~ x_vals,
        y = ~ y_vals,
        z = ~ z_vals) %>%
  add_surface(colorsscale = 'Viridis', contours = list(
     x = list(
      show = TRUE,
      usecolormap = TRUE,
      highlightcolor = "#ff0000",
      project = list(x= TRUE)
    ),
    z = list(
      show = TRUE,
      usecolormap = TRUE,
      highlightcolor = "#ff0000",
      project = list(z = TRUE)
    )
  )) %>%
  layout(title = "3D Surface Plot of p(x,y) <- exp(-xy)",
         scene = list(
           xaxis = list(title = 'x'),
           yaxis = list(title = 'y'),
           zaxis = list(title = 'Density')
         ))


contour(
  x_vals,
  y_vals,
  z_vals,
  xlab = 'x',
  ylab = 'y',
  main = expression("p(x,y) % propto ~ e^{-xy}"),
  nlevels = 20,
  col = 'blue'
)


z_df <- expand.grid(x = x_vals, y = y_vals)

z_df$z <- exp(-z_df$x * z_df$y)

ggplot(z_df, aes(x = x, y = y, z = z)) +
  geom_contour_filled(binds = 20) +
  labs(
    title = expression("p(x,y) %propto ~ e^{-xy}"),
    x = 'x',
    y = 'y',
    fill = 'Density'
  ) + ßß
theme_minimal()

ggplot(z_df, aes(x = x, y = y, color = z)) +
  geom_point(alpha = 0.7, size = 1) +
  scale_color_viridis_c(option = 'plasma') +
  labs(
    title = expression("p(x, y) %propto e^{-x * y})"),
    x = 'x',
    y = 'y',
    fill = 'Density'
  ) +
  theme_minimal()
